//
//  UIColor+SetColor.m
//  tools
//
//  Created by hyjet on 2018/3/14.
//  Copyright © 2018年 tools. All rights reserved.
//

#import "UIColor+SetColor.h"

@implementation UIColor (SetColor)

UIColor* cw_colorForHex(NSString *hexString){
    if (!hexString || [hexString isEqualToString:@""]) {
        return nil;
    }
    //去掉所有空格
    NSString *str = [hexString stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (str.length<7) {
        return nil;
    }
    unsigned red,green,blue;
    NSRange range;
    
    range.length = 2;
    range.location = 1;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
    
    range.location = 3;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
    
    range.location = 5;
    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
    UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];
    return color;
}

UIColor* cw_colorForRGB(NSInteger r,NSInteger g,NSInteger b,NSInteger a){
    UIColor *color= [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a];
    return color;
}

+ (UIColor *)cw_colorForHex:(NSString *)hexString{
    return cw_colorForHex(hexString);
}

+ (UIColor *)cw_colorForRGB:(NSInteger)r green:(NSInteger)g blue:(NSInteger)b alpha:(NSInteger)a
{
    return cw_colorForRGB(r,g,b,a);
}
@end
